package org.fast.easy.messages.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.fast.easy.core.domain.messages.MessagePublishDomain;
import org.fast.easy.core.domain.result.R;
import org.fast.easy.framework.websocket.utils.WebSocketUtils;
import org.fast.easy.messages.service.IMessageService;
import org.springframework.stereotype.Service;

/**
 * 短信服务
 *
 * @author Lion Li
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MessageServiceImpl implements IMessageService {

    /**
     * 发送消息
     *
     * @param sessionKey session主键 一般为用户id
     * @param message    消息文本
     */
    @Override
    public void sendMessage(Long sessionKey, String message) {
        WebSocketUtils.sendMessage(sessionKey, message);
    }

    /**
     * 发布订阅的消息(群发)
     *
     * @param message 消息内容
     */
    @Override
    public void publishAll(String message) {
        WebSocketUtils.publishAll(message);
    }

    @Override
    public boolean publishAll(MessagePublishDomain message) {
        boolean flag = false;
        if(message!=null){
            String type = message.getType();
            String title = message.getTitle();
            if(StringUtils.isNotBlank(type) && StringUtils.isNotBlank(title)){
                publishAll("[" + type + "] " + title);
                flag = true;
            }
        }
        return flag;
    }

}
